package com.luzj.utility;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * Created by Administrator on 2016/9/18.
 */
public class JsoupURL {
    //初始种子URL 游民星空
      final private static String YOUMIN = "http://www.gamersky.com/";
      private static boolean flag= true;

    private static   LinkedList<String> caches = new LinkedList<>();
    private static Set<String> visits = new HashSet<>();
    private static LinkedList<String> bbses = new LinkedList<>();

    private static int count = 0;

    /**
     * 获取单页面所有链接
     * @param seed
     * @return
     */
      public static LinkedList<String> cacheURL(String seed){
          LinkedList<String> cachedUrls = new LinkedList<>();
        Document doc = null;
        try {
            doc = Jsoup.parse(new URL(seed).openStream(), "UTF-8",seed);
        } catch (IOException e) {
                e.printStackTrace();
                System.out.print("fuck");
        }
              Elements links =  doc.select("a[href]");
              for (Element link : links){
                  String url =link.attr("href");
                  if(JsoupURL.isUrl(url)&&!cachedUrls.contains(url)){
                      cachedUrls.add(url);
//                    System.out.println(url);
                  }
              }
          return cachedUrls;
 }

    /**
     * 拿到固定url种子，解析目标网页信息
     * @return 游戏信息集合 HashMap > name type score
     * 注：目前只写到 抓取name 先进行持久化工作 回头再补抓取其他信息的问题
     * @throws IOException
     */
    public Map<String,String> parseInfo() throws Exception {
            Map<String,String> gameInfo =  new HashMap<>();
            LinkedList<String> urls = JsoupURL.cacheURL(YOUMIN);
        for(String url : urls){
            System.out.println(url);
            Document doc =Jsoup.parse(new URL(url).openStream(),"UTF-8",url);
            //游戏名称
            Element title ;
            String gameName = "none";
            //共三种页面布局 第一种抓取
            title = doc.select("div.CHtit").first();
            //第二种抓取
            if(title==null){
//                System.out.println("==CHtit is null==");
                Element element = doc.getElementsByClass("YXXX_2").first();
                if(element!=null){
                    title = element.child(0);
                }
            }
//            第三种抓取
            if(title == null){
//                System.out.println("==YXXX2 is null==");
                Element element =doc.select("ul.YXXX").first();
                if(element!=null){
                    title = element.child(0).child(1);
                }
            }
            //抓取名称信息
            if(title!=null && title.hasText()){
                gameName = title.text();
            }

            //游戏类型
//            Element type = doc.select("ul.YXXX").first();
            //游民测评指数
//            Element youminIndex = doc.select("pnum").first();
            if(!gameName.equals("none")){
                gameInfo.put("gameName", gameName);
                System.out.println(gameName);
            }
        }
             return gameInfo;
    }



    /**
     * 广度遍历url种子
     * @return
     */
    public Map<String,String> parseURL(){
        //首次调用爬取首页url,初始化
        if(flag){
            caches = JsoupURL.cacheURL(YOUMIN);
            flag= false;
        }
        String parsedURL ;
        while((parsedURL =caches.poll())!=null){
            if("".equals(parsedURL)){
                continue;
            }
            LinkedList<String> tems = JsoupURL.cacheURL(parsedURL);
            for(String seed : tems){
                if(!visits.contains(seed)&&!caches.contains(seed)){
                    caches.add(seed);
                    count++;
                    if(count%50==0){
                        System.out.println(count);
                    }
//                    System.out.println(seed.toString());
                }
            }

            visits.add(parsedURL);
        }
        return null;
    }

    /**
     * 是否是需要的url类型
     * @param url 种子url
     * @return
     */
    private static boolean isUrl(String url){
        //专区主页面 http://www.gamersky.com/z/thewitcher3/
         final String PC ="http://www\\.gamersky\\.com/z/([a-zA-Z0-9])+/";
        //下载页面，各种参数 http://down.gamersky.com/pc/201405/365773.shtml
        final String DOWN ="http://down\\.gamersky\\.com/pc/([0-9/])+.shtml";
        //BBS论坛下载地址页面 http://bbs.gamersky.com/thread-2489004-1-1.html#Soft
        final String BBS = "http://bbs\\.gamersky\\.com/thread\\-([a-zA-Z0-9\\-])+.html#Soft";
        if(url.matches(PC)){
            return true;
        }
        return false;
    }

    //测试
        public static void main(String[] args) throws Exception {
                new JsoupURL().parseInfo();
        }
}
